<?php
App::uses('AppModel', 'Model');
/**
 * Article Model
 *
 * @property SysSeo $SysSeo
 * @property ArticleCate $ArticleCate
 * @property User $User
 */
class Article extends AppModel {
	
	public $actsAs = array('Pagination');

/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'title';

/**
 * Validation rules
 *
 * @var array
 */
	public $validate = array(
		'sys_website_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'article_cate_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'user_id' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'title' => array(
			'notEmpty' => array(
				'rule' => array('notEmpty'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'publish' => array(
			'date' => array(
				'rule' => array('date'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'sort' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
		'sort_top' => array(
			'numeric' => array(
				'rule' => array('numeric'),
				//'message' => 'Your custom message here',
				//'allowEmpty' => false,
				//'required' => false,
				//'last' => false, // Stop validation after this rule
				//'on' => 'create', // Limit validation to 'create' or 'update' operations
			),
		),
	);

	//The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
	public $belongsTo = array(
		'SysWebsite' => array(
				'className' => 'SysWebsite',
				'foreignKey' => 'sys_website_id',
				'conditions' => '',
				'fields' => '',
				'order' => ''
		),
		'SysSeo' => array(
			'className' => 'SysSeo',
			'foreignKey' => 'sys_seo_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'ArticleCate' => array(
			'className' => 'ArticleCate',
			'foreignKey' => 'article_cate_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'User' => array(
			'className' => 'User',
			'foreignKey' => 'user_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);
	
	/**
	 * 分页条件
	 * @author mingxing.zhao@qq.com
	 * @Date 2012-12-20
	 * @return Array
	 */
	function pageParam($action){
		return array(
				"colNames"=>array(
						'ID','站点','资讯标题','分类','资讯作者','资讯来源','编辑','点击','排序','创建时间','操作'
				),
				"colModel"=>array(
						array("name"=>'Article.id',"width"=>'50',"sortable"=>true,"align"=>"right"),
						array("name"=>'Article.sys_website_id',"width"=>"80","sortable"=>true,"align"=>"center"),
						array("name"=>'Article.title',"width"=>"200","sortable"=>true,"align"=>"left"),
						array("name"=>'Article.article_cate_id',"width"=>"100","sortable"=>true,"align"=>"center"),
						array("name"=>'Article.auth',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'Article.resource',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'Article.user_id',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'Article.count_view',"width"=>"50","sortable"=>true,"align"=>"center"),
						array("name"=>'Article.sort',"width"=>"30","sortable"=>true,"align"=>"center"),
						array("name"=>'Article.created',"width"=>"110","sortable"=>true,"align"=>"center"),
						array("name"=>'act',"width"=>"150","sortable"=>false,"align"=>"center")
				),
				"action"=>$action,
				'sortname'=>'id',
				'sortorder'=>'DESC'
		);
	}
	
	/**
	 * 分页数据
	 * @author mingxing.zhao@qq.com
	 * @param array
	 * @date 2013-12-20
	 * @return json
	 *
	 */
	function jsonData($res){
		$rowN = array();
		$i=0;
		foreach($res['rows'] as $k => $value){
			$rowN[$i]['id'] = $value['Article']['id'];
			$rowN[$i++]['cell'] = array(
					$value['Article']['id'],
					$value['SysWebsite']['title'],
					$value['Article']['title'],
					$value['ArticleCate']['title_zh'],
					$value['Article']['auth'],
					$value['Article']['resource'],
					$value['User']['name'],
					$value['Article']['count_view'],
					$value['Article']['sort'],
					$value['Article']['created'],
					''
			);
		}
		$res['rows'] = $rowN;
		return json_encode($res);
	}
	
	function relateArticle($tags,$limit = 5,$not = false){
		if(strlen($tags)==0){
			return array();
		}else{
			$tagArr = split(',', $tags);
			$conditions = array("OR"=>array(),'Article.enable'=>1);
			foreach ($tagArr as $tag){
				$tag = trim($tag);
				$conditions['OR'][] = array('Article.tags like'=>'%'.$tag.'%');
			}
			if($not){
				$conditions['Article.id <>'] = $not;
			}
			$this->recursive = -1;
			return $this->find('all',array(
				'fields'=>array('Article.id','Article.title','Article.article_cate_id','Article.publish','Article.enable'),
				'conditions'=>$conditions,
				'limit'=>$limit,
				'order'=>'Article.publish DESC',
				'group'=>'Article.id'
			));
		}
	}
	
	function newArticle($limit = 6){
		return $this->find('all',array(
				'fields'=>array('Article.id','Article.title','Article.article_cate_id','Article.publish','Article.enable'),
				'limit'=>$limit,
				'order'=>'Article.created DESC',
		));
	}
}
